package org.hackystat.devcathlon.engine.event.impl;

import static org.junit.Assert.assertEquals;

import javax.xml.datatype.XMLGregorianCalendar;

import org.hackystat.devcathlon.configuration.ConfigurationManager;
import org.hackystat.devcathlon.engine.event.Event;
import org.hackystat.devcathlon.engine.event.EventScore;
import org.hackystat.devcathlon.engine.event.EventScoreManager;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.test.DevcathlonTestHelper;
import org.hackystat.sensorbase.resource.projects.jaxb.Project;
import org.hackystat.utilities.tstamp.Tstamp;
import org.junit.Test;
import org.junit.BeforeClass;

/**
 * Test Keep The Repository Clean Hourly Event.
 * 
 * @author Ka Hung Phillip Lau
 */
public class TestKeepTheRepositoryCleanHourly extends DevcathlonTestHelper {

  /** The actual failed build weight. */
  private static int failedWeight = -5;
  
  private String teamName = "TestTeam";
  private String teamOwner = "TeamOwner";
  private String matchName = "TestMatch";
  private String member1 = "Member1";
  private String member2 = "Member2";
  
  /**
   * Sets up the DevcathlonConfiguration for use in these test cases. 
   * @throws Exception If problems occur setting up the configuration. 
   */
  @BeforeClass
  public static void setupConfiguration() throws Exception {
    // Set up the testing DevcathlonConfiguration for KeepTheRepositoryCleanHourly.
    String eventName = KeepTheRepositoryCleanHourly.eventName;
    ConfigurationManager configManager = ConfigurationManager.getInstance();
    configManager.setBaseScoreWeight(eventName, 1);
    configManager.getConfiguration(eventName);
    configManager.setWakeupInterval(eventName, 60);
    configManager.addProperty(eventName, KeepTheRepositoryCleanHourly.failedWeightKey, 
        String.valueOf(failedWeight));
  }
  
  /**
   * Tests this event when there is no sensor data for this team.
   * @throws Exception Unexpected exception.
   */
  @Test
  public void testNoData() throws Exception {
    // Create the team. 
    Team team = makeTeam(teamName, teamOwner, member1, member2);
    
    // Now create the event instance.
    Event event = new KeepTheRepositoryCleanHourly();
    
    // Create a simple match containing a single team, a single project, and this event.
    Match match = addSimpleMatch(matchName, team, getProject(teamOwner, teamName), event);
    
    // Test the event with no sensor data.
    XMLGregorianCalendar timestamp = Tstamp.makeTimestamp();
    EventScore score = event.computeScore(match, team, timestamp);
    EventScoreManager.getInstance().updateEventScore(score);
    assertEquals("Check base 1", 0, score.getBaseScore());
    assertEquals("Check points 1", 0, event.computePoints(score));
    
    // Cleanup.
    deleteTeam(teamName);
    deleteMatch(matchName);
  }
  
  /**
   * Tests this event when there is a single build and the build is success. 
   * @throws Exception If there is a bug.
   */
  @Test
  public void testSingleBuildSuccess() throws Exception {
    // Create the team. 
    Team team = makeTeam(teamName, teamOwner, member1, member2);
    
    // Now create the event instance.
    Event event = new KeepTheRepositoryCleanHourly();
    
    // Create a simple match containing a single team, a single project, and this event.
    Project project = getProject(teamOwner, teamName);
    Match match = addSimpleMatch(matchName, team, project, event);
    
    // Send a single build data.
    makeBuildData(team.getOwner(), project, Tstamp.makeTimestamp(), true);
    
    // Now compute the event.
    XMLGregorianCalendar timestamp = Tstamp.makeTimestamp();
    EventScore score = event.computeScore(match, team, timestamp);
    EventScoreManager.getInstance().updateEventScore(score);
    assertEquals("Check base 2", 0, score.getBaseScore());
    assertEquals("Check points 2", 0, event.computePoints(score));
    
    // Cleanup.
    deleteTeam(teamName);
    deleteMatch(matchName);
  }
  
  /**
   * Tests this event when there is multiple builds and builds is success.
   * @throws Exception If there is a bug.
   */
  @Test
  public void testMultipleBuildSuccess() throws Exception {
    // Create the team. 
    Team team = makeTeam(teamName, teamOwner, member1, member2);
    
    // Now create the event instance.
    Event event = new KeepTheRepositoryCleanHourly();
    
    // Create a simple match containing a single team, a single project, and this event.
    Project project = getProject(teamOwner, teamName);
    Match match = addSimpleMatch(matchName, team, project, event);
    
    // Send successful builds
    for (int i = 0; i < 7; i++) {
      makeBuildData(team.getOwner(), project, Tstamp.makeTimestamp(), true);  
    }   
    
    // Now compute the event.
    XMLGregorianCalendar timestamp = Tstamp.makeTimestamp();
    EventScore score = event.computeScore(match, team, timestamp);
    EventScoreManager.getInstance().updateEventScore(score);
    assertEquals("Check base 3", 0, score.getBaseScore());
    assertEquals("Check points 3", 0, event.computePoints(score));
    
    // Cleanup.
    deleteTeam(teamName);
    deleteMatch(matchName);
  }
  
  /**
   * Tests this event when there is a single build and build is failed.
   * @throws Exception If there is a bug.
   */
  @Test
  public void testSingleBuildFailed() throws Exception {
    // Create the team. 
    Team team = makeTeam(teamName, teamOwner, member1, member2);
    
    // Now create the event instance.
    Event event = new KeepTheRepositoryCleanHourly();
    
    // Create a simple match containing a single team, a single project, and this event.
    Project project = getProject(teamOwner, teamName);
    Match match = addSimpleMatch(matchName, team, project, event);
    
    // Send a build followed by failed build
    makeBuildData(team.getOwner(), project, Tstamp.makeTimestamp(), false);
    
    // Now compute the event.
    XMLGregorianCalendar timestamp = Tstamp.makeTimestamp();
    EventScore score = event.computeScore(match, team, timestamp);
    EventScoreManager.getInstance().updateEventScore(score);
    assertEquals("Check base 4", -5, score.getBaseScore());
    assertEquals("Check points 4", -5, event.computePoints(score));
    
    // Cleanup.
    deleteTeam(teamName);
    deleteMatch(matchName);
  }
  
  /**
   * Tests this event when there is multiple builds and builds is failed.
   * @throws Exception If there is a bug.
   */
  @Test
  public void testMultipleBuildFailed() throws Exception {
    // Create the team. 
    Team team = makeTeam(teamName, teamOwner, member1, member2);
    
    // Now create the event instance.
    Event event = new KeepTheRepositoryCleanHourly();
    
    // Create a simple match containing a single team, a single project, and this event.
    Project project = getProject(teamOwner, teamName);
    Match match = addSimpleMatch(matchName, team, project, event);
    
    // Send failed builds
    for (int i = 0; i < 7; i++) {
      makeBuildData(team.getOwner(), project, Tstamp.makeTimestamp(), false);  
    }   
    
    // Now compute the event.
    XMLGregorianCalendar timestamp = Tstamp.makeTimestamp();
    EventScore score = event.computeScore(match, team, timestamp);
    EventScoreManager.getInstance().updateEventScore(score);
    assertEquals("Check base 5", -5, score.getBaseScore());
    assertEquals("Check points 5", -5, event.computePoints(score));
    
    // Cleanup.
    deleteTeam(teamName);
    deleteMatch(matchName);
  }
  
  /**
   * Tests this event when there is multiple success and failed builds.
   * @throws Exception If there is a bug.
   */
  @Test
  public void testMultipleSuccessAndFailed() throws Exception {
    // Create the team. 
    Team team = makeTeam(teamName, teamOwner, member1, member2);
    
    // Now create the event instance.
    Event event = new KeepTheRepositoryCleanHourly();
    
    // Create a simple match containing a single team, a single project, and this event.
    Project project = getProject(teamOwner, teamName);
    Match match = addSimpleMatch(matchName, team, project, event);
    
    // Send failed builds
    for (int i = 0; i < 7; i++) {
      makeBuildData(team.getOwner(), project, Tstamp.makeTimestamp(), false);  
    }   
    
    // Send successful builds
    for (int l = 0; l < 7; l++) {
      makeBuildData(team.getOwner(), project, Tstamp.makeTimestamp(), true);  
    }
    
    // Now compute the event.
    XMLGregorianCalendar timestamp = Tstamp.makeTimestamp();
    EventScore score = event.computeScore(match, team, timestamp);
    EventScoreManager.getInstance().updateEventScore(score);
    assertEquals("Check base 6", -5, score.getBaseScore());
    assertEquals("Check points 6", -5, event.computePoints(score));
    
    // Cleanup.
    deleteTeam(teamName);
    deleteMatch(matchName); 

  }
  
}
